define(function(){
    var currPageObj={};
    currPageObj.init=function(){
        var self = this;
        queryPageList(1);
    	
        currPageObj.initEvents();
    };

    //绑定事件
    currPageObj.initEvents=function(){
        var self = this;
        $(".page-info").on("click","li",function(){
    		var topage = $(this).attr("page");
    		if(topage != "#"){
    			queryPageList(topage);
    		}
    	});
        
        $("#pageSize").on("change",function(){
        	queryPageList(1);
    	});
    	
    	$("#searchBtn").click(function() {
    		queryPageList(1);
    	});
    };
    return currPageObj;
});

function freshData() {
	var topage = $(".page-info .cur-page").attr("page");
	if(topage != "#" && topage !="undefined"){
		queryPageList(topage);
	}
}

var curPage=0;
var totalPage=0;
function queryPageList(currentpage) {
	var pageSize = $("#pageSize").val();
	var userName = $("#searchUserName").val();
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/log/getLogPageList.htm',
		data : 'userName=' + userName+'&currentpage=' + currentpage+'&pageSize='+pageSize,
		cache : false,
		success : function(data) {
			
			if(data.error){
				return;
			}
			
			var res =  data.data;
			var pagelist = res.datas;
			curPage = res.pageNumber;
			totalPage = res.totalPages;
			totalCount = res.total;
			var listString = "";
			$("#pageDataList").empty();
			for(var i=0; i<pagelist.length; i++){
					var listItem = pagelist[i];
					var rank = parseInt((curPage-1)*pageSize+i*1+1);
					var stLogId = listItem.stLogId;
					var userName = listItem.stUserName;
					var stIp = listItem.stIp;
					var dtLog = FormatDate(listItem.dtLog);
                	var stType = listItem.stType;
					var stDetail = listItem.stDetail;
					
					if(i % 2 == 1){
						listString+="<tr class='even'>";
					}else{
						listString+=" <tr>";
					}
					listString+="    <td >"+rank+"</td>";
					listString+="    <td><span title="+userName+">"+ userName+ "</span></td>";
					listString+="    <td><span title="+stIp+">"+ stIp+ "</span></td>";
					listString+="    <td><span title="+dtLog+">"+ dtLog+ "</span></td>";
                	listString+="    <td><span title="+stType+">"+ stType+ "</span></td>";
					listString+="    <td><span title="+stDetail+">"+ stDetail+ "</span></td>";
					listString+=" </tr>";
			}
			$("#pageDataList").append(listString);
			$("#totalCount").html(totalCount);
			setPageInfoPart(parseInt(curPage),parseInt(totalPage));
		},
		error : function() {
		}
	});
}

function mouseOver(tip)
{
	$("#"+tip).css("display","inline-block");
}

function mouseOut(tip)
{
	$("#"+tip).css("display","none");
}
//设置页码
function setPageInfoPart(curPage, totalPage) {
	$(".page-info .page").remove();
	var startPage = curPage - 5;//要显示的起始页码
	var endPage = curPage + 5;
	
	if(startPage<=0){
		startPage = 1;
	}
	if(endPage >= totalPage){
		endPage = totalPage;
	}
	var showPageNum = endPage - startPage+1;//要显示的页码总数
	//添加页码元素
	var lastElem = $(".page-info .pre");
	var count = 0;
	if(startPage>1){
		$(
				"<li page=1 class='page'>1</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(curPage - 1>=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	for ( var i = count; i < showPageNum+count; i++) {
		if (curPage == startPage) {
			$(
					"<li page=\""+startPage+"\" class=\"page cur-page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		} else {
			$(
					"<li page=\""+startPage+"\" class=\"page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		}
		lastElem = $(".page-info .page:eq(" + i + ")");
		startPage++;
	}
	count = i;
	
	if(totalPage - curPage >=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(endPage<totalPage){
		$(
				"<li page="+totalPage+" class='page'>"+totalPage+"</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + totalPage + ")");
		count++;
	}
	//设置首页，上一页，下一页，最后一页页码
	if(curPage == 1){
		$(".page-info .first").css("display","none");
		$(".page-info .pre").css("display","none");
	}else{
		$(".page-info .first").css("display","inline-block");
		$(".page-info .pre").css("display","inline-block");
		
		
		$(".page-info .first").attr("page", 1);
		$(".page-info .pre")
				.attr("page", (curPage - 1) > 0 ? (curPage - 1) : 1);
	}
	if(curPage == totalPage){
		
		$(".page-info .next").css("display","none");
		$(".page-info .last").css("display","none");
	}else{
		$(".page-info .next").css("display","inline-block");
		$(".page-info .last").css("display","inline-block");
		$(".page-info .next").attr("page",
				(curPage + 1) > totalPage ? totalPage : (curPage + 1));
		$(".page-info .last").attr("page", totalPage);
	}

}

function FormatDate (strTime) {
	if(strTime == "") return "";
    var unixtimestamp = new Date(strTime);
    var year = 1900 + unixtimestamp.getYear();
    var month = "0" + (unixtimestamp.getMonth() + 1);
    var date = "0" + unixtimestamp.getDate();
    var hour = "0" + unixtimestamp.getHours();
    var minute = "0" + unixtimestamp.getMinutes();
    var second = "0" + unixtimestamp.getSeconds();
    return year + "-" + month.substring(month.length-2, month.length)  + "-" + date.substring(date.length-2, date.length)
        + " " + hour.substring(hour.length-2, hour.length) + ":"
        + minute.substring(minute.length-2, minute.length) + ":"
        + second.substring(second.length-2, second.length);
}


